using Digitalmes.Domain.Entities.Mes.Equipments;

namespace Digitalmes.Application.Mes.Equipments.Alarms.Queries;

/// <summary>
/// 设备警报定义项分页查询命令。
/// </summary>
public sealed record GetAlarmItemListWithPagedQuery(
    string? LineCode,
    string? WorkCenter,
    int PageNumber,
    int PageSize) : IPagedQuery, IRequest<IResult<PagedList<EquipAlarmItemDto>>>;

internal sealed class GetAlarmItemListWithPagedQueryHandler(IRepository<EquipAlarmItem> repo) : IRequestHandler<GetAlarmItemListWithPagedQuery, IResult<PagedList<EquipAlarmItemDto>>>
{
    public async Task<IResult<PagedList<EquipAlarmItemDto>>> Handle(GetAlarmItemListWithPagedQuery request, CancellationToken cancellationToken)
    {
        var items = await repo.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(request.LineCode), m => m.LineCode == request.LineCode)
            .WhereIF(!string.IsNullOrEmpty(request.WorkCenter), m => m.WorkCenter == request.WorkCenter)
            .OrderBy(m => m.LineCode).OrderBy(m => m.WorkCenter).OrderBy(m => m.Index)
            .GetPagedListAsync(request.PageNumber, request.PageSize, cancellationToken);
        return Result.From(items.Adapt<PagedList<EquipAlarmItemDto>>());
    }
}
